package com.mr.cai.main;

import android.content.Context;
import android.text.TextUtils;

import com.cai.mr.lib.utils.LogInfo;
import com.mr.cai.main.manager.ActivityManager;
import com.mr.cai.main.manager.PreferencesManager;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.UsingFreqLimitedMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;

import org.litepal.LitePalApplication;

/**
 * Created by hai8108 on 16/2/5.
 */
public class MrCaiApplication extends LitePalApplication{
    private static MrCaiApplication instance;
    private String customerId;
    @Override
    public void onCreate () {
        super.onCreate();
        instance = this;
        LogInfo.setIsDebug(true);
        initImageLoader(this);
    }

    public static MrCaiApplication getInstance () {
        return instance;
    }

    public static void initImageLoader (Context context) {
        ImageLoaderConfiguration config = new ImageLoaderConfiguration
                .Builder(context)
                .memoryCacheExtraOptions(480, 800) // max width, max height，即保存的每个缓存文件的最大长宽
//				.discCacheExtraOptions(480, 800, CompressFormat.JPEG, 75, null) // Can slow ImageLoader, use it carefully (Better don't use it)/设置缓存的详细信息，最好不要设置这个
                .threadPoolSize(3)//线程池内加载的数量
                .threadPriority(Thread.NORM_PRIORITY - 2)
                .denyCacheImageMultipleSizesInMemory()
                .memoryCache(new UsingFreqLimitedMemoryCache(2 * 1024 * 1024)) // You can pass your own memory cache implementation/你可以通过自己的内存缓存实现
                .memoryCacheSize(2 * 1024 * 1024)
                .diskCacheSize(50 * 1024 * 1024)
                .tasksProcessingOrder(QueueProcessingType.LIFO)
                .diskCacheFileNameGenerator(new Md5FileNameGenerator())
                .defaultDisplayImageOptions(DisplayImageOptions.createSimple())
                .imageDownloader(new BaseImageDownloader(context, 5 * 1000, 30 * 1000)) // connectTimeout (5 s), readTimeout (30 s)超时时间
                .writeDebugLogs() // Remove for release app
                .build();//开始构建
        // Initialize ImageLoader with configuration.
        ImageLoader.getInstance().init(config);
    }

    public String getCustomerId () {
        if(TextUtils.isEmpty(customerId)){
            customerId = PreferencesManager.getInstance().getUserCustomerId();
        }
        return customerId;
    }

    public void exitProcess(){
        ActivityManager.destory();
        android.os.Process.killProcess(android.os.Process.myPid());
    }
}
